import { createRouter, createWebHistory } from 'vue-router'

// 路由懒加载
const Login = () => import('@/views/Login.vue')
const Home = () => import('@/views/Home.vue')
const Dashboard = () => import('@/views/Dashboard.vue')
const CustomerManagement = () => import('@/views/CustomerManagement.vue')
const ActivityManagement = () => import('@/views/ActivityManagement.vue')
const UserManagement = () => import('@/views/UserManagement.vue')
const RoleManagement = () => import('@/views/RoleManagement.vue')
const SystemLog = () => import('@/views/SystemLog.vue')

// 新增路由组件
const LeadList = () => import('@/views/LeadList.vue')
const LeadPool = () => import('@/views/LeadPool.vue')
const OpportunityList = () => import('@/views/OpportunityList.vue')
const PublicPool = () => import('@/views/PublicPool.vue')
const CourseManagement = () => import('@/views/CourseManagement.vue')
const DepartmentManagement = () => import('@/views/DepartmentManagement.vue')
const Profile = () => import('@/views/Profile.vue')

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { requiresAuth: true }
      },
      {
        path: '/customer-management',
        name: 'CustomerManagement',
        component: CustomerManagement,
        meta: { requiresAuth: true }
      },
      {
        path: '/activity-management',
        name: 'ActivityManagement',
        component: ActivityManagement,
        meta: { requiresAuth: true }
      },
      // 新增路由
      {
        path: '/lead-list',
        name: 'LeadList',
        component: LeadList,
        meta: { requiresAuth: true }
      },
      {
        path: '/lead-pool',
        name: 'LeadPool',
        component: LeadPool,
        meta: { requiresAuth: true }
      },
      {
        path: '/opportunity-list',
        name: 'OpportunityList',
        component: OpportunityList,
        meta: { requiresAuth: true }
      },
      {
        path: '/public-pool',
        name: 'PublicPool',
        component: PublicPool,
        meta: { requiresAuth: true }
      },
      {
        path: '/course-management',
        name: 'CourseManagement',
        component: CourseManagement,
        meta: { requiresAuth: true }
      },
      {
        path: '/department-management',
        name: 'DepartmentManagement',
        component: DepartmentManagement,
        meta: { requiresAuth: true }
      },
      {
        path: '/user-management',
        name: 'UserManagement',
        component: UserManagement,
        meta: { requiresAuth: true }
      },
      {
        path: '/role-management',
        name: 'RoleManagement',
        component: RoleManagement,
        meta: { requiresAuth: true }
      },
      {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: { requiresAuth: true }
      },
      {
        path: '/system-log',
        name: 'SystemLog',
        component: SystemLog,
        meta: { requiresAuth: true }
      }
    ]
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要登录
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 获取token（注意：使用正确的token键名）
    const token = localStorage.getItem('token')
    if (!token) {
      // 没有token，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 有token，继续访问
      next()
    }
  } else {
    // 不需要登录的页面直接访问
    next()
  }
})

export default router